//
//  TRRequest.m
//  SafePay
//
//  Created by  ztryxh on 2018/6/15.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "TRRequest.h"
#import <SVProgressHUD.h>
#import "NSDictionary+add.h"
@implementation TRRequest
+(instancetype)request{
    return [[self alloc]init];
}
-(instancetype)init{
    
    self=[super init];
    if(self){
        
        self.operationManager=[AFHTTPSessionManager manager];
    }
    return self;
}
-(void)Get:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(TRRequest *, NSDictionary *))success failure:(void (^)(TRRequest *, NSError *))failure{
    
     BOOL isShowCycle=YES;

    if([URLString containsString:kMyProperty]||[URLString containsString:kCoinDetails]){
           
           isShowCycle=NO;
       }
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor clearColor]];
    [SVProgressHUD setForegroundColor:kSetColor(@"2182fc")];
    if(isShowCycle){
        
        [SVProgressHUD show];
    }
    self.operationQueue=self.operationManager.operationQueue;
    self.operationManager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [self.operationManager GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%@",downloadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(isShowCycle){
            
            [SVProgressHUD dismiss];
            
        }
        
        NSString *resposonJson=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"[TRRequest] Success: %@",resposonJson);
        success(self,[self convertjsonStringToDict:resposonJson]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if(isShowCycle){
            
            [SVProgressHUD dismiss];
            
        }
        NSLog(@"[TRRequest] fail: %@",error.localizedDescription);
        failure(self,error);
        
    }];
}
-(void)Post:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(TRRequest *, NSDictionary *))success failure:(void (^)(TRRequest *, NSError *))failure
{
    BOOL isShowCycle=YES;
    
    if([URLString containsString:kMyProperty]||[URLString containsString:kCoinDetails]){
        
        isShowCycle=NO;
    }
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor clearColor]];
    [SVProgressHUD setForegroundColor:kSetColor(@"2182fc")];
    if(isShowCycle){
        
        [SVProgressHUD show];
    }
    NSLog(@"url\n%@\npara\n%@",URLString,parameters);
    self.operationQueue = self.operationManager.operationQueue;
    self.operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [self.operationManager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"%@",uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(isShowCycle){
            
            [SVProgressHUD dismiss];
            
        }
        NSString* responseJson = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"[TRRequest] success: %@",responseJson);
        
        success(self,[self convertjsonStringToDict:responseJson]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if(isShowCycle){
            
            [SVProgressHUD dismiss];
            
        }
        NSLog(@"[TRRequest] fail: %@",error.localizedDescription);
        failure(self,error);
        
    }];
    
}
-(void)cancelAllOperations{
    
    
    [self.operationQueue cancelAllOperations];
}
-(NSDictionary *)convertjsonStringToDict:(NSString *)jsonString{
    
    NSDictionary *retDict = nil;
    if ([jsonString isKindOfClass:[NSString class]]) {
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        retDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
        
        return  [NSDictionary changeType:retDict];
    }else{
        return [NSDictionary changeType:retDict];
    }
    
}
-(UIViewController*)currentViewController{
    //获得当前活动窗口的根视图
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1)
    {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}
@end
